package com.huika.renrench.control.main.activity;

import java.util.HashSet;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.CustomViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.huika.renrench.R;
import com.huika.renrench.config.Constant;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.main.fragment.EntrepreneurshipFragment;
import com.huika.renrench.control.main.fragment.InvestmentFragment;
import com.huika.renrench.control.more.fragment.MoreFragment;
import com.huika.renrench.control.my.fragment.MyFragment;
import com.huika.renrench.support.helps.VersionManager;
import com.huika.renrench.view.TitleBarHelper;

/**
 * @author chenyuan
 * @date：2015年6月1日,下午2:17:30
 * @describe:人人创主页activity
 */
public class MainActivity extends BaseAct implements OnPageChangeListener {
	/** 选中的fragment适配器 */
	SectionsPagerAdapter mSectionsPagerAdapter;
	/** viewPager */
	CustomViewPager mViewPager;
	/** 底部的布局 */
	private LinearLayout page_indicator;
	/** 初始化的下标 */
	private HashSet<Integer> initFrags;
	/** 全部标题 */
	private String[] titleStrs;
	private TitleBarHelper titleBarHelper;

	@Override
	public void setRootView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_index);
		titleBarHelper = new TitleBarHelper(this, -1,
				-1, R.string.common_null);
	}

	@Override
	protected void initWidget() {
		// TODO Auto-generated method stub
		super.initWidget();
		vm = new VersionManager(context);
		
		titleStrs = getResources().getStringArray(
				R.array.renrenchuan_page_indicator);
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());

		mViewPager = (CustomViewPager) findViewById(R.id.pager);
		mViewPager.setCanDragState(false);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		mViewPager.setOnPageChangeListener(this);

		page_indicator = (LinearLayout) findViewById(R.id.page_indicator);
		int tabCount = page_indicator.getChildCount();
		for (int i = 0; i < tabCount; i++) {
			page_indicator.getChildAt(i).setTag(i);
			page_indicator.getChildAt(i).setOnClickListener(this);
		}

		initFrags = new HashSet<Integer>();
		initFrags.add(0);
		onPageSelected(0);

	}

	@Override
	public void onPageSelected(int item) {
		final int tabCount = page_indicator.getChildCount();
		for (int i = 0; i < tabCount; i++) {
			final View child = page_indicator.getChildAt(i);
			final boolean isSelected = (i == item);
			child.setSelected(isSelected);
		}
		setTitles(item);
	}

	private void setTitles(int position) {
		titleBarHelper.setTitleMsg(titleStrs[position]);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		int index = (Integer) v.getTag();
		if (index != mViewPager.getCurrentItem()) {
			mViewPager.setCurrentItem(index);
			onPageSelected(index);
		}
	}

	/** fragment的适配器类 */
	public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			Fragment frag = null; 
			switch (position) {
			case 0:
				frag = new EntrepreneurshipFragment();
				break; 
			case 1:
				frag = new InvestmentFragment();
				break;
			case 2:
				frag = new MyFragment();
				break;
			case 3:
				frag = new MoreFragment();
				break;
			} 
			return frag; 
		} 

		@Override
		public int getCount() {
			return titleStrs.length;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	protected void onResume() {
		super.onResume();
		VersionManager();
	}
	
	private VersionManager vm;
	
	/**检查版本更新*/
	private void VersionManager(){
		vm.checkVersion(false , false);
	}
	
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			isExit = false;
		};
	};
	/** 是否退出应用 */
	private boolean isExit;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (!isExit) {
				showToastMsg("再按一次退出应用");
				isExit = true;
				mHandler.sendEmptyMessageDelayed(0, 1000);
				return true;
			} else
				sendBroadcast(new Intent(Constant.ACTION_EXIT_SYSTEM));
		}
		return super.onKeyDown(keyCode, event);
	};
	
}
